package tech.aistar.corejava.day12.map;

import java.util.*;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/7/30
 * @Version: 1.0
 */
public class MyForMapDemo {
    public static void main(String[] args) {
        Map<Integer,String> map = new HashMap<>();

        //调用put方法
        //针对key是无序的不可重复的,如果有重复的,那么后面的会覆盖前面的
        //这一点需要和hashset进行区分[拒绝添加]

        map.put(1,"java");
        map.put(2,"python");
        map.put(1,"mysql");//会覆盖java
        map.put(3,"oracle");
        map.put(4,"redis");

        //map底层是重写了toString方法
        String mapString = map.toString();
        System.out.println(mapString);

        System.out.println("=========");

        Set<MyEntry<Integer,String>> set = handlerMap(mapString);

        Iterator<MyEntry<Integer,String>> iter = set.iterator();
        while(iter.hasNext()){
            MyEntry<Integer,String> e =iter.next();
            Integer key = e.getKey();
            String value = e.getValue();
            System.out.println(key + ":" + value);
        }
    }

    private static Set<MyEntry<Integer,String>> handlerMap(String mapString) {
        //创建一个Set集合
        Set<MyEntry<Integer,String>> set = new HashSet<>();
        //{1=mysql, 2=python, 3=oracle, 4=redis}
        //去{}
        mapString = mapString.substring(1,mapString.length() - 1);
        //1=mysql, 2=python, 3=oracle, 4=redis
        System.out.println(mapString);

        //根据指定的正则表达式来进行切割
        String[] arr = mapString.split(",");
        for (String s : arr) {
            System.out.println(s);
        }
        for (String group : arr) {
            String[] gp =group.trim().split("=");

            System.out.println(gp[0]+":"+gp[1]);
            //将这两个数据封装到容器中 - 对象容器
            //实体类就是用来封装数据的
            MyEntry<Integer,String> entry = new MyEntry<>(Integer.parseInt(gp[0]),gp[1]);//?
            set.add(entry);
        }
        return set;
    }
}
